<?php
/**
 * 商家 订单回调
 * Created by PhpStorm.
 * User: soshow
 * Date: 17/3/28
 * Time: 下午6:31
 */
class store_notifyControl extends Control{


    public function wxNotifyOp(){

        $order_sn = $_POST['out_trade_no']; //商家单号
        $trade_no = $_POST['trade_no']; //微信支付单号
        $total_fee = $_POST['total_fee'];
        /** @var orderModel $model */
        $model = Model('order');
        $orderInfo = $model->getOrderInfo(array('order_sn'=>$order_sn,'order_status'=>10));

        if(!empty($orderInfo)) {
            $time = time();
            $rand_code = '';
            for ($i = 0; $i < 16; $i++) {
                $rand_code .= mt_rand(0, 9);
            }
            $ticket_code = str_pad($orderInfo['member_id'].($orderInfo['order_id']+$orderInfo['member_id']+$orderInfo['merchant_id']),16,$rand_code);
            $res = $model->updateOrder(array('order_id'=>$orderInfo['order_id']), array(
                'order_status'=>20,
                'true_pay'=>$total_fee/100,
                'pay_sn' => $trade_no,
                'pay_type'=>1,
                'pay_time'=>$time,
                'ticket_code'=>$ticket_code,
            ));

            if ($res) {
                //发送推送信息
                $param = array(
                    'order_sn'=>$orderInfo['order_sn'],
                    'order_name'=>$orderInfo['ticket_title'],
                    'order_amount'=>$total_fee/100,
                );
                //发送短信
                if($orderInfo['order_type']==1){
                    //酒店
                    $msg = '您好,您预订的'.$orderInfo['ticket_title'].','.$orderInfo['spec_name'].',订单号'.$orderInfo['order_sn'].'支付成功！'.'请待后台工作人员确认是否有房,稍后我们将通过短信的方式通知您,请您注意已查收！感谢您使用玩转六盘水APP！';
                }else{
                    $msg = '您好,您预订的'.$orderInfo['ticket_title'].'订单号'.$orderInfo['order_sn'].'下单成功,'.'核销码为：'.$ticket_code.'。请凭该核销码及时使用。感谢您使用玩转六盘水APP！';
                }
                require_once BASE_DATA_PATH.'/vendors/clSms/ChuanglanSmsHelper/ChuanglanSmsApi.php';
                $clapi  = new ChuanglanSmsApi();
                //查询用户真实手机号
                $member_model = Model('member');
                $mb_ = $member_model->getMemberInfo(array('member_id'=>$orderInfo['member_id']),'mobile');
                $clapi->sendSMS($mb_['mobile'],$msg);
                //$this->wx_send_add_order($orderInfo['member_id'],$param);
            }
        }
    }



    /**
     * 支付成功订单
     * @return bool
     */
    public function wx_send_add_order($member_id,$param){
        include_once BASE_DATA_PATH.'/vendors/weixin_share/jssdk.php';
        $jssdk = new JSSDK();
        $access_token = $jssdk->getToken();
        /** @var memberModel $member */
        $member = Model('member');
        $memberInfo = $member->getMemberInfo(array('member_id'=>$member_id));
        if($memberInfo['openid']){
            $template_id = "UiBw-gQ55QylWEQdRgCjhvbESDNUAkKAryal4smziDk";
            $data = array(
                'first'=>array('value'=>'您的订单已支付成功！'),
                'keyword1'=>array('value'=>$param['order_sn']),
                'keyword2'=>array('value'=>$param['order_name']),
                'keyword3'=>array('value'=>$param['order_amount']),
                'remark'=>array('value'=>'支付成功！如有疑问请致电。！'),
            );
            $url = "http://yin.lvbangw.com/index.php?act=member&op=order";
            $result = $jssdk->doSend($memberInfo['openid'],$template_id,$url,$data,$access_token);
            return $result;
        }else{
            return false;
        }
    }

    /**
     * 支付宝回调
     */
    public function alipayOp()
    {
        $trade_no = $_POST['trade_no'];//支付宝交易单号
        $out_trade_no = $_POST['out_trade_no'];//
        $receipt_amount = $_POST['receipt_amount'];

        /** @var orderModel $model */
        $model = Model('order');
        $orderInfo = $model->getOrderInfo(array('order_sn'=>$out_trade_no,'order_status'=>10));
        if(!empty($orderInfo)) {
            $time = time();
            $rand_code = '';
            for ($i = 0; $i < 16; $i++) {
                $rand_code .= mt_rand(0, 9);
            }
            $ticket_code = str_pad($orderInfo['member_id'].($orderInfo['order_id']+$orderInfo['member_id']+$orderInfo['merchant_id']),16,$rand_code);
            $res = $model->updateOrder(array('order_id'=>$orderInfo['order_id']), array(
                'order_status'=>20,
                'true_pay'=>$receipt_amount,
                'pay_sn' => $trade_no,
                'pay_time'=>$time,
                'pay_type'=>2,
                'ticket_code'=>$ticket_code,
            ));

            if($res){
                //发送短信
                if($orderInfo['order_type']==1){
                    //酒店
                    $msg = '您好,您预订的'.$orderInfo['ticket_title'].','.$orderInfo['spec_name'].',订单号'.$orderInfo['order_sn'].'支付成功！'.'请待后台工作人员确认是否有房,稍后我们将通过短信的方式通知您,请您注意已查收！感谢您使用玩转六盘水APP！';
                }else{
                    $msg = '您好,您预订的'.$orderInfo['ticket_title'].'订单号'.$orderInfo['order_sn'].'下单成功,'.'核销码为：'.$ticket_code.'。请凭该核销码及时使用。感谢您使用玩转六盘水APP！';
                }
                require_once BASE_DATA_PATH.'/vendors/clSms/ChuanglanSmsHelper/ChuanglanSmsApi.php';
                $clapi  = new ChuanglanSmsApi();
                //查询用户真实手机号
                $member_model = Model('member');
                $mb_ = $member_model->getMemberInfo(array('member_id'=>$orderInfo['member_id']),'mobile');
                $clapi->sendSMS($mb_['mobile'],$msg);
            }
            $res && exit('success');
        }
    }
}